package com.travel.server.manager.impl.command.project.data;

import com.travel.common.CommonConstant;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.project.data.QueryProjectCostRequest;
import com.travel.server.manager.dto.response.project.data.QueryProjectCostResponse;
import com.travel.server.manager.dto.response.project.data.QueryProjectCostResponse.CostItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.ProjectDal;
import com.travel.server.manager.impl.dal.ProjectDal.ProjectParam;
import com.travel.server.manager.impl.dal.SpecialDal;
import com.travel.server.manager.impl.dal.SpecialDal.SpecialParam;
import com.travel.server.manager.impl.sal.ProjectCostSal;
import com.travel.server.manager.model.ProjectInfo;
import com.travel.server.manager.model.ProjectSpecial;
import com.travel.server.manager.model.dto.data.OrderCostItemDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/29 12:12
 * @Desc:
 */
@Component
@Slf4j
public class QueryProjectCostCommand extends AbstractCommand<QueryProjectCostRequest, QueryProjectCostResponse> {

    @Autowired
    private SpecialDal specialDal;

    @Autowired
    private ProjectDal projectDal;

    @Override
    protected void checkInput(QueryProjectCostRequest request) {
        checkTimeInput(request);
    }

    @Override
    public QueryProjectCostResponse handle(QueryProjectCostRequest request) {
        checkInput(request);

        Date startTime = DateUtil.getDataTime(request.getStartTime());
        Date endTime = DateUtil.getDataEndTime(request.getEndTime());

        List<CostItem> costList = new ArrayList<>();

        List<ProjectSpecial> specialList = specialDal.selectSpecial(startTime, endTime, request.getOffset(), request.getLimit());
        int totalNum = specialDal.selectSpecialCount(startTime, endTime);
        if (!CollectionUtil.isEmpty(specialList)) {
            List<OrderCostItemDto> costItemList = specialDal.selectOrderCostItem(startTime, endTime);
            //从订单统计出来的，暂时不用，需求说明，项目费=订单数*项目费，申请费=订单数*申请费
            Map<Integer, Integer> projectMap = ProjectCostSal.makeCostMap(costItemList, 1);
            Map<Integer, Integer> applyMap = ProjectCostSal.makeCostMap(costItemList, 2);
            Map<Integer, Integer> discountMap = ProjectCostSal.makeCostMap(costItemList, 3);
            Map<Integer, Integer> countMap = ProjectCostSal.makeCostMap(costItemList, 4);
            Map<Integer, String> nameMap = projectDal.makeProjectName(specialList);
            for (ProjectSpecial projectSpecial : specialList) {
                Integer id = projectSpecial.getId();
                CostItem item = new CostItem();
                int specialCost = projectSpecial.getPartner() + projectSpecial.getDiscount() + projectSpecial.getCompensate() + projectSpecial.getInsurance() + projectSpecial.getTravel()
                        + projectSpecial.getMaterial();
                Integer orderCost = discountMap.get(id);
                int cost = specialCost + (orderCost == null ? 0 : orderCost);
                item.setCost(CommonUtil.fenToYuan(cost).toString());
                item.setId(id);
                String name = nameMap.get(projectSpecial.getProjectId());
                item.setProject(name == null ? "" : name);
                Integer count = countMap.get(id);
                count = count == null ? 0 : count;
                item.setTravelNum(count + "");
                item.setTravelDate(projectSpecial.getTravelDate());
                Long orderProject = count * projectSpecial.getPrice();
                Integer specialProject = projectSpecial.getProject();
                specialProject = specialProject == null ? 0 : specialProject;
                Long project = specialProject + orderProject;
                item.setProjectAmount(CommonUtil.fenToYuan(project).toString());
                Integer orderApply = count * projectSpecial.getApplyAmount();
                Integer specialApply = projectSpecial.getApply();
                specialApply = specialApply == null? 0 : specialApply;
                Integer apply = specialApply + orderApply;
                item.setApplyAmount(CommonUtil.fenToYuan(apply).toString());
                costList.add(item);
            }
        }

        QueryProjectCostResponse response = new QueryProjectCostResponse();
        response.setCostList(costList);
        response.setTotalNum(totalNum);

        if(CommonConstant.YES.equals(request.getIsCount())){
            Date currentMonthFirst = DateUtil.getMonthFirstDay(0);
            Date currentMonthLast = DateUtil.getMonthLastDay(1);
            Date lastMonthFirst = DateUtil.getMonthFirstDay(-1);
            Date lastMonthLast = DateUtil.getMonthLastDay(0);

            Integer currentMonth = specialDal.selectMonthCost(currentMonthFirst, currentMonthLast);
            Integer lastMonth = specialDal.selectMonthCost(lastMonthFirst, lastMonthLast);
            if(currentMonth != null) {
                response.setCurrentMonth(CommonUtil.fenToYuan(currentMonth).toString());
            }
            if(lastMonth != null) {
                response.setLastMonth(CommonUtil.fenToYuan(lastMonth).toString());
            }
        }else{
            response.setCurrentMonth(null);
            response.setLastMonth(null);
        }

        return response;
    }
}
